/**
 * Created by Administrator on 2019/3/23.
 */
var table;
var parentCode;
$(function () {
    parentCode = locations.ParamString("parentCode");
    table = $("#rolesList");
    //initComponent();
    initGrid();
    $("#queryRolesBtn").click(function () {
        table.datagrid("reload");
    });
    $("#restoreBtn").click(function () {
        $("form[name='queryForm']").form('reset');
        initComponent();
        table.datagrid("reload");
    });
});
function initComponent() {
    var curr_time = new Date();
    var strDate = curr_time.getFullYear() + "-";
    strDate += curr_time.getMonth() + 1 + "-";
    strDate += curr_time.getDate() + " 00:00:00";
    $("#startTime").datetimebox("setValue", strDate);
    $("#endTime").datetimebox("setValue", addDate(curr_time, 0) + " 23:59:59");
}
var initGrid = function () {
    var param = queryButtonLimit(parentCode);
    table.datagrid({
        iconCls : 'icon-ok',
        pageSize : 10,
        pageList : [ 10, 20, 40, 80 ],
        striped : true,
        collapsible : false,
        remoteSort : true,// 服务器端排序
        fitColumns : false,
        singleSelect : true,
        showFooter : true,
        columns : [[{
            field : 'id',
            checkbox : true,
            hidden:true
        }, {
            field : 'roleName',
            title : '角色名称',
            width : 150,
            align : 'center'
        }, {
            field : 'sign',
            title : '角色标识',
            width : 120,
            align : 'center',
            formatter:function (value, row, index) {
                if(value == "default") {
                    return "默认";
                } else if(value == "superadmin") {
                    return "超级管理员";
                } else if(value == "admin") {
                    return "管理员";
                } else if(value == "staff") {
                    return "员工";
                }
            }
        }, {
            field : 'status',
            title : '状态',
            width : 200,
            align : 'center',
            formatter:function (value, row, index) {
                if(value == "on") {
                    return "启用";
                } else if(value == "off") {
                    return "停用";
                }
            }
        }, {
            field : 'createNickname',
            title : '创建人',
            width : 120,
            align : 'center'
        }, {
            field : 'created',
            title : '创建时间',
            width : 200,
            align : 'center',
            formatter:function (value, row, index) {
                if(row.created == null) {
                    return "";
                }
                return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.created));
            }
        }, {
            field : 'updateNickname',
            title : '修改人',
            width : 120,
            align : 'center'
        }, {
            field : 'updated',
            title : '修改时间',
            width : 200,
            align : 'center',
            formatter:function (value, row, index) {
                if(row.updated == null) {
                    return "";
                }
                return dateFtt("yyyy-MM-dd hh:mm:ss",new Date(row.updated));
            }
        }
        ]],
        toolbar:toolbar,
        pagination : true,
        rownumbers : true,
        toolbar : param,
        onBeforeLoad : function(param) {
            var parameter = new Object();
            parameter.page = param.page;
            parameter.rows = param.rows;
            if($("#roleName").val()!="") parameter.roleName = $("#roleName").val();
            if($("#sign").val()!="") parameter.sign = $("#sign").val();
            if($("#status").val()!="") parameter.status = $("#status").val();
            if($("#remark").val()!="") parameter.remark = $("#remark").val();
            if($("#startTime").val()!="") parameter.startTime = $("#startTime").val();
            if($("#endTime").val()!="") parameter.endTime = $("#endTime").val();
            var jsonstr = postSyncAjax("/selectRolesByPage/normal", parameter);
            //console.log(jsonstr);
            //console.log(jsonstr.data);
            var json = jsonstr.data;
            var dataGridData = new Object();
            if(null != json && undefined !=json) {
                dataGridData.rows = json.rows;
                dataGridData.total = json.total;
                table.datagrid('loadData', dataGridData);
            }
        }
    });

}

var addRoles = function (uri) {
    openMessageZone(uri);
}
var editRoles = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    var id = rows.id;
    showThePage(parentCode, uri, {
       url : uri + "/" + id
    });
}
var queryRoles = function () {
    table.datagrid("reload");
}
var removeRoles = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    // 查询是否不属于管理员
    var id = rows.id;
    $.messager.confirm("确认对话框","确定要删除该角色信息？",function (r) {
        if(r){
            var jsonstr = postSyncAjax(uri+'/'+id, null);
            console.log(jsonstr);
            if(jsonstr.success) {
                $.messager.alert("消息提示", "角色删除成功！", "warning");
                table.datagrid("reload");
            } else {
                $.messager.alert("消息提示", "角色删除失败："+jsonstr.msg, "warning");
            }
        }
    });
}
var allotLimits = function (uri) {
    var rows = table.datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("消息提示", "至少选择一条记录", "warning");
        return;
    }
    var id = rows.id;
    showThePage(parentCode, uri, {
        url : uri + "/" + id
    });
}